package com.kpmg.questionnaire.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.io.Serializable;

/**
 * 
 * @author Hanks
 *
 */
public final class WebUtils {

    private WebUtils() {
    }

    /**
     * @Method getHttpServletRequest
     * @Description TODO
     * @return
     * @return HttpServletRequest
     * @since 1.0.0
     */
    public static HttpServletRequest getHttpServletRequest() {
        ServletRequestAttributes reqAttr = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (reqAttr == null) {
            return null;
        }
        return reqAttr.getRequest();
    }

    /**
     * @Method getSessionObject
     * @Description TODO
     * @param cls
     * @param key
     * @return
     * @return T
     * @since 1.0.0
     */
    public static <T extends Serializable> T getSessionObject(Class<T> cls, String key) {
        HttpServletRequest request = getHttpServletRequest();
        if (request == null) {
            return null;
        }
        return getSessionObject(getHttpServletRequest().getSession(), cls, key);
    }

    /**
     * @Method getSessionObject
     * @Description 获取session 信息
     * @param request
     * @param cls
     * @param key
     * @return
     * @return T
     * @since 1.0.0
     */
    public static <T extends Serializable> T getSessionObject(HttpServletRequest request, Class<T> cls, String key) {
        return getSessionObject(request.getSession(), cls, key);
    }

    /**
     * @Method getSessionObject
     * @Description TODO
     * @param session
     * @param cls
     * @param key
     * @return
     * @return T
     * @since 1.0.0
     */
    @SuppressWarnings("unchecked")
    public static <T extends Serializable> T getSessionObject(HttpSession session, Class<T> cls, String key) {
        return (T) session.getAttribute(key);
    }

    /**
     * @Method responseAjax
     * @Description 处理界面返回内容
     * @param response
     * @param operResult
     * @return void
     * @since 1.0.0
     */
    public static void responseAjax(HttpServletResponse response, Object operResult) {
        response.setContentType("application/json; charset=utf-8");
        response(response, JSONObject.toJSONString(operResult));
    }
    
    public static void responseHtml(HttpServletResponse response, String operResult) {
        response.setContentType("text/html; charset=utf-8");
        response(response, operResult);
    }
    
    private static void response(HttpServletResponse response, String operResult) {
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.write(operResult);
        } catch (Exception e) {
        } finally {
            IOUtils.closeQuietly(out);
        }
    }
}